<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    {% load static %}
    <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
    <script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <h2 class="text-center">登陆</h2>
            <div class="form-group">
                <label for="id_username">用户名</label>
                <input type="text" name="username" class="form-control" id="id_username">
            </div>
            <div class="form-group">
                <label for="id_password">密码</label>
                <input type="password" name="password" class="form-control" id="id_password">
            </div>
            <div class="form-group">
                <label for="id_code">验证码</label>
                <div class="row">
                    <div class="col-md-6">
                        <input type="text" id="id_code" name="code" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <img src="/get_code/" alt="" height="35" width="360" id="id_img">
                    </div>
                </div>
            </div>
            <button class="btn btn-success" id="id_submit">登陆</button>
            <span class="errors" style="color: red"></span>
        </div>
    </div>
</div>

<script>
    $('#id_img').click(function () {
        // 获取src属性 在此基础之上 修改一下即可
        var oldSrc = $(this).attr('src');
        $(this).attr('src', oldSrc + '?')
    });

    $('#id_submit').click(function () {
        $.ajax({
            url: '',
            type: 'post',
            data: {
                'username': $('#id_username').val(),
                'password': $('#id_password').val(),
                'code': $('#id_code').val(),
                'csrfmiddlewaretoken': '{{ csrf_token }}'
            },
            success: function (data) {
                if (data.code == 100) {
                    location.href = data.url
                } else {
                    $('.errors').text(data.msg)
                }
            }


        })
    })
</script>
</body>
</html>